package com.blog.admin.controller;


import com.blog.admin.service.SysPermissionsService;
import com.blog.admin.vo.SysMenuVO;
import com.blog.admin.vo.SysRoleVO;
import com.blog.admin.vo.SysUserVO;
import com.blog.util.Result;
import com.github.pagehelper.PageInfo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.blog.util.Result.beanToMap;


/**
 * 用户角色菜单权限模块
 */
@RestController
@RequestMapping("sysPermissionsController")
public class SysPermissionsController {
    @Autowired
    SysPermissionsService sysPermissionsService;


    /**
     * 获取当前用户角色
     * @return
     */
    @RequestMapping("getRole")
    public Map<String,Object> getRole(){
        SysUserVO loginName =  (SysUserVO)SecurityUtils.getSubject().getPrincipal();
        SysRoleVO sysRoleVO = sysPermissionsService.getRoleById(loginName.getUsername());
        return Result.responeAjaxMap(true, "成功", sysRoleVO,null);
    }

    /**
     * 获取左侧菜单
     */
    @RequestMapping("getTree")
    public Map<String,Object> getTree(){
        SysUserVO loginName =  (SysUserVO)SecurityUtils.getSubject().getPrincipal();
        SysRoleVO sysRoleVO = sysPermissionsService.getRoleById(loginName.getId());
        List<Map<String,Object>>  tree = sysPermissionsService.getThisRoleMenu(sysRoleVO);
        Map<String,Object> code = new HashMap<>();
        code.put("code",200);
        return Result.responeAjaxMap(true, "成功", tree,code);
    }

    /**
     *  点击权限分配 初始化当前角色
     */
    @RequestMapping("getPermissions")
    public Map<String,Object> getPermissions(String roleId){
        SysRoleVO sysRoleVO = sysPermissionsService.getMenuById(roleId);
        boolean result = sysPermissionsService.initPermission(sysRoleVO);
        List<Map<String,Object>>  tree = sysPermissionsService.getThisRoleMenu(sysRoleVO);
        return Result.responeAjaxMap(result, "成功", tree,null);
    }

    /**
     * 修改权限分配
     */
    @RequestMapping("updatePermissions")
    public Map<String,Object> updatePermissions(String nodesData){
       /* SysRoleVO sysRoleVO = new SysRoleVO();
        sysRoleVO.setId(roleId);
        sysPermissionsService.initPermission(sysRoleVO);
        sysPermissionsService.updatePermissions(JSONArray.fromObject(permission),roleId);*/
        sysPermissionsService.updatePermissions(JSONArray.fromObject(nodesData));
        return Result.responeAjaxMap(true, "成功", null,null);
    }


    /**
     *
     * 获取菜单路径
     */
    @RequestMapping("getMenuList")
    public Map<String,Object> getMenuList(){
        List<Map<String,Object>> sysMenuVOS = sysPermissionsService.getAllMeu();
       /* Map<String,Object> data = new HashMap<>();
        data.put("count",sysMenuVOS.size());*/
        return Result.responeAjaxMap(true, "ok", sysMenuVOS,null);
    }

    /**
     *
     * 获取菜单下拉框
     */
    @RequestMapping("getMenuListCheck")
    public Map<String,Object> getMenuListCheck(String id){
        List<Map<String,Object>> sysMenuVOS = sysPermissionsService.getAllMeu();
        for (int i=0;i<sysMenuVOS.size();i++){
                if (sysMenuVOS.get(i).get("authorityId").equals(id)){
                    sysMenuVOS.remove(i);
                }
        }
        return Result.responeAjaxMap(true, "ok", sysMenuVOS,null);
    }


    /**
     * 插入菜单
     * @param sysMenuVO
     * @return
     */
    @RequestMapping("insertMenu")
    public Map<String,Object> insertMenu(SysMenuVO sysMenuVO){
        boolean result = sysPermissionsService.insertMenu(sysMenuVO);
        return Result.responeAjaxMap(result, "操作成功", "",null);
    }

    /**
     * 修改菜单
     * @param sysMenuVO
     * @return
     */
    @RequestMapping("updateMenu")
    public Map<String,Object> updateMenu(SysMenuVO sysMenuVO){
        boolean result = sysPermissionsService.updateMenu(sysMenuVO);
        return Result.responeAjaxMap(result, "操作成功", "",null);
    }

    /**
     * 删除菜单
     * @param sysMenuVO
     * @return
     */
    @RequestMapping("deleteMenu")
    public Map<String,Object> deleteMenu(SysMenuVO sysMenuVO){
        boolean result = sysPermissionsService.deleteMenu(sysMenuVO);
        return Result.responeAjaxMap(result, "操作成功", "",null);
    }

    /**
     * 获取角色列表
     * @return
     */
    @RequestMapping("getRoleList")
    public Map<String,Object> getRoleList(HttpServletRequest request){
        Map<String,Object> data = new HashMap<>();
        String page = request.getParameter("page");
        String size = request.getParameter("limit");
        if (StringUtils.isEmpty(page)){
            page = "0";
            size = "0";
        }
        PageInfo<SysRoleVO> result = sysPermissionsService.getRoleList(Integer.valueOf(page) ,Integer.valueOf(size));
        data.put("count",result.getTotal());
        data.put("code",0);
        List<Map<String,Object>> list = new ArrayList<>();
        for (SysRoleVO sysRoleVO : result.getList()){
            list.add(beanToMap(sysRoleVO));
        }
        return Result.responeAjaxMap(true, "ok", list,data);
    }

    /**
     * 插入角色
     * @param sysRoleVO
     * @return
     */
    @RequestMapping("insertRole")
    public Map<String,Object> insertRole(SysRoleVO sysRoleVO){
        boolean result = sysPermissionsService.insertRole(sysRoleVO);
        return Result.responeAjaxMap(result, "操作成功", "",null);
    }

    /**
     * 修改角色
     * @param sysRoleVO
     * @return
     */
    @RequestMapping("updateRole")
    public Map<String,Object> updateRole(SysRoleVO sysRoleVO){
        boolean result = sysPermissionsService.updateRole(sysRoleVO);
        return Result.responeAjaxMap(result, "操作成功", "",null);
    }

    /**
     * 删除角色
     * @param sysRoleVO
     * @return
     */
    @RequestMapping("deleteRole")
    public Map<String,Object> deleteRole(SysRoleVO sysRoleVO){
        boolean result = sysPermissionsService.deleteRole(sysRoleVO);
        return Result.responeAjaxMap(result, "操作成功", "",null);
    }



}
